package com.wzdigit.wms.wms.strategy.webservice.config;
/*
 * @Author:yangwenbiao
 * @Date 2021/3/15 10:49 AM
 * @version 1.0
 */

import com.wzdigit.wms.basic.enums.writeback.TopicEnum;
import com.wzdigit.wms.wms.strategy.webservice.handle.SendWebServiceHandle;
import com.wzdigit.wms.wms.strategy.webservice.iservice.ISendWebService;
import com.wzdigit.wms.wms.strategy.webservice.type.SendWebService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class SendWebServiceConfig implements ApplicationListener<ContextRefreshedEvent> {
//public class SendWebServiceListener implements ApplicationContextAware {

    private static Map<TopicEnum, ISendWebService> sendWebServiceMap = new HashMap<>();

//    @Override
//    public void onApplicationEvent(ContextRefreshedEvent event) {
//        ApplicationContext applicationContext = event.getApplicationContext();
//
//        Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(SendWebService.class);
//
//        if (beansWithAnnotation != null) {
//            sendWebServiceMap = new HashMap<>();
//
//            for (Map.Entry<String, Object> entry : beansWithAnnotation.entrySet()) {
//                Object value = entry.getValue();
//                Class<?> aClass = value.getClass();
////                SendWebService annotation = aClass.getAnnotation(SendWebService.class);
//                SendWebService declaredAnnotation = aClass.getDeclaredAnnotation(SendWebService.class);
//                SendWebService annotation = AnnotationUtils.getAnnotation(aClass, SendWebService.class);
//                SendWebService annotation1 = aClass.getAnnotation(SendWebService.class);
////                SendWebService[] annotationsByType = aClass.getAnnotationsByType(SendWebService.class);
////                SendWebService annotation = annotationsByType[0];
//                String key = annotation.value();
//
//                sendWebServiceMap.put(key, (ISendWebService) value);
//            }
//
////            beansWithAnnotation.forEach((key, value) -> {
////                String bizType = value.getClass().getAnnotation(SendWebService.class).name();
////                sendWebServiceMap.put(bizType, (ISendWebService) value);
////            });
//        }
//    }

    public Object send(TopicEnum topicEnum,Object object) {
        return sendWebServiceMap.get(topicEnum).send(object);
    }

//    @Override
//    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//
//        Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(SendWebService.class);
//
//
//        //获取所有策略注解的Bean
//        Map<String, Object> orderStrategyMap = applicationContext.getBeansWithAnnotation(SendWebService.class);
//
//        for (Map.Entry<String, Object> entry : orderStrategyMap.entrySet()) {
//            Object value = entry.getValue();
//            Class<SendWebService> orderStrategyClass = (Class<SendWebService>) value.getClass();
//
//            String key = orderStrategyClass.getAnnotation(SendWebService.class).value();
//            //将class加入HandlerOrderContext的map中,type作为key
////            HandlerOrderContext.orderStrategyBeanMap.put(type,orderStrategyClass);
//            System.out.println(key);
//            sendWebServiceMap.put(key, (ISendWebService) value);
//        }
//
//
//
//        orderStrategyMap.forEach((k, v) -> {
//            //获取策略实现类
//            Class<SendWebService> orderStrategyClass = (Class<SendWebService>) v.getClass();
//            //获取策略实现类的注解值。
//            String key = orderStrategyClass.getAnnotation(SendWebService.class).value();
//            //将class加入HandlerOrderContext的map中,type作为key
////            HandlerOrderContext.orderStrategyBeanMap.put(type,orderStrategyClass);
//            System.out.println(key);
//            sendWebServiceMap.put(key, (ISendWebService) v);
//        });
//    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {


        ApplicationContext applicationContext = event.getApplicationContext();

        //获取所有策略注解的Bean
        Map<String, Object> orderStrategyMap = applicationContext.getBeansWithAnnotation(SendWebService.class);

        for (Map.Entry<String, Object> entry : orderStrategyMap.entrySet()) {
            Object value = entry.getValue();
            Class<SendWebService> orderStrategyClass = (Class<SendWebService>) value.getClass();

            TopicEnum key = orderStrategyClass.getAnnotation(SendWebService.class).value();

            SendWebServiceHandle.push(key, (ISendWebService) value);
        }

    }
}
